#include "buffer_object.h"

OPEN_JLIB_NS


BufferObject::~BufferObject () {
  this->clear();
}


bool BufferObject::init (BufferType type, uwl size, BufferMode mode, const void* data) {
  clear();
  _type = (GLenum)type;
  glGenBuffers(1, &_id);
  glBindBuffer(_type, _id);
  glBufferData(_type, size, data, (GLenum)mode);
  return true;
}


void BufferObject::set (swl offset, uwl size, const void* data) {
  debug_assert(valid());
  glBindBuffer(_type, _id);
  glBufferSubData(_type, (GLintptr)offset, (GLsizeiptr)size, data);
}


void BufferObject::use () {
  debug_assert(valid());
  glBindBuffer(_type, _id);
}


void BufferObject::clear () {
  if (this->valid()) {
    glDeleteBuffers(1, &_id);
    _id = 0;
  }
}


void* BufferObject::_map (GLenum mode) {
  debug_assert(valid());
  glBindBuffer(_type, _id);
  return glMapBuffer(_type, mode);
}

void BufferObject::_unmap () {
  glUnmapBuffer(_type);
}


CLOSE_JLIB_NS
